#!/usr/bin/php
<?php

/**
 * Helper command line toolkit to convert DOS line endings to Unix line endings
 *
 * @category  Pone
 * @package   Pone_Tools
 * @author    Pham Cong Dinh <pcdinh at phpvietnam dot net>
 * @since     October 27, 2008
 * @copyright Copyright (c) 2007-2008 PHPVietnam Group (http://www.phpvietnam.net)
 * @license   http://phpvietnam.net/license/gpl3
 * @version   $Id: line_ending_fix.php 5 2008-10-27 17:13:48Z pcdinh $
 */

/**
 * This tool can be executed from command line or web access
 *
 * @example php line_ending_fix.php
 * @example php line_ending_fix.php 'path_to_source_code_dir'
 * @example php line_ending_fix.php 'path_to_source_code_dir' > /dev/null
 */

// Change path to base path of the source code files here
// Unix path: '/home/webdocs/deployment/framework/Pone/Database'
// Windows path: 'D:\wvbsrc\repos\pone\trunk\incubator\Pone\Database'
$config['source_base_path'] = 'D:\wvbsrc\repos\pone\trunk\incubator\Pone\Database';

if (false === isset($_SERVER['argv'][1]))
{
	// Base path the source code directory
	$basePath = $config['source_base_path'];
}
else
{
	$basePath = $_SERVER['argv'][1];
}

try
{
	$it = new RecursiveDirectoryIterator($basePath);

	foreach (new RecursiveIteratorIterator($it) as $file)
	{
		if (true  === $it->isDot())
		{
			continue;
		}

		$path     = $file->getPathname();

		$pathInfo = pathinfo($path);

		if (true  === isset($pathInfo['extension']))
		{
			switch ($pathInfo['extension'])
			{
				case 'php':
				case 'css':
				case 'js':
					file_put_contents($path, _fixCRLF($path));
					break;

				default:
					// no-op
			}
		}
	}
}
catch (Exception $ex)
{
	echo $ex->getMessage();
}

/**
 * Converts DOS-style line endings (CR+LF) to Unix line endings (LF character)
 *
 * Fix a bug that can cause error message: "inconsistent line ending style" when commiting files
 * to Subversion repository that property svn:eol-style is set
 *
 * Common results are that Unix programs treat the CR character present in
 * Windows files as a regular character (usually rendered as ^M),
 * and that Windows programs combine all of the lines of a Unix file into
 * one giant line because no carriage return-linefeed (or CRLF) character combination
 * was found to denote the end of line
 *
 * Unix line endings (LF character): \n    [line feed]
 * DOS-style line endings (CR+LF)  : \r\n  [carriage return]
 *
 * @category  Pone
 * @package   Pone_Tools
 * @author    Pham Cong Dinh <pcdinh at phpvietnam dot net>
 * @since     October 27, 2008
 * @copyright Copyright (c) 2007-2008 PHPVietnam Group (http://www.phpvietnam.net)
 * @license   http://phpvietnam.net/license/gpl3
 * @version   $Id: line_ending_fix.php 5 2008-10-27 17:13:48Z pcdinh $
 */

/**
 * @see      *.php = svn:mime-type=text/plain;svn:eol-style=LF;svn:keywords=Date Author Id Revision HeadURL LastChangedDate
 * @internal Prefixing function names to make it internal use
 */
function _fixCRLF($path)
{
	$data      = file_get_contents($path);

	if (false !== $data)
	{
		$data  = str_replace(array("\r\n", "\r"), "\n", $data, $count);
		error_log('Read '.$path.' and '.$count.' characters are replaced. ', null, dirname(__FILE__).'/rs.log');
	}
	else
	{
		error_log('Could not read from '.$path, null, dirname(__FILE__).'/error.log');
	}

	return $data;
}
?>